package org.apache.catalina.util;

import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.ResourceBundle;

public class CharsetToUtf8 {
    /**
     * todo 聂志鹏 这个方法是我自己添加的，用来支持以中文打印tomcat的启动消息
     */
    public static void convert(ResourceBundle bnd){
        // bnd为空，或者bnd的目标locale就是英语，则不转换
        if (bnd == null){
            return;
        }
        try {
            Field lookupField = bnd.getClass().getDeclaredField("lookup");
            lookupField.setAccessible(true);
            Map<String,Object> lookup = (Map<String,Object>)lookupField.get(bnd);
            if (lookup == null) {
                return;
            }
            for (Map.Entry<String, Object> entry : lookup.entrySet()) {
                if (entry.getValue() != null){
                    entry.setValue(new String(String.valueOf(entry.getValue()).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
                }
            }
        }catch (Exception e){
            // 忽略异常
            e.printStackTrace();
        }
    }
}
